package legumes.shared.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Id;

import com.googlecode.objectify.annotation.Serialized;

public class OrderPart implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id private long id;
	@Serialized private List<OrderPartProductItem> orderPartProductItems;
	private OrderPartMenuItem orderPartMenuItem;

	public OrderPart() {
		orderPartMenuItem = new OrderPartMenuItem();
		orderPartMenuItem.setMenuCount(MenuCount.ZERO_MENU);
		orderPartProductItems = new ArrayList<OrderPartProductItem>();
	}

	/**
	 * @return the orderPartMenuItem
	 */
	public OrderPartMenuItem getOrderPartMenuItem() {
		return orderPartMenuItem;
	}

	/**
	 * @param orderPartMenuItem
	 *            the orderPartMenuItem to set
	 */
	public void setOrderPartMenuItem(OrderPartMenuItem orderPartMenuItem) {
		this.orderPartMenuItem = orderPartMenuItem;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[OrderPart id=").append(getId()).append(" productItems=");
		for (OrderPartProductItem lc : getOrderPartProductItems()) {
			sb.append(lc.toString()).append(" ");
		}
		sb.append(" menuItem=").append(getOrderPartMenuItem().toString());
		sb.append("]");
		return sb.toString();
	}

	public Cost computePrice() {
		Cost cost = new Cost();
		if( orderPartProductItems != null ){
			for (OrderPartProductItem oi : orderPartProductItems) {
				cost.plus(oi.computePrice());
			}
			cost.plus(getOrderPartMenuItem().getPrice());
		}
		return cost;
	}

	public Cost computePriceWithoutMenu() {
		Cost cost = new Cost();
		if( orderPartProductItems != null ){
			for (OrderPartProductItem oi : orderPartProductItems) {
				cost.plus(oi.computePrice());
			}
		}
		return cost;
	}

	public long getId() {
		return id;
	}

	public void setId(long id_) {
		id = id_;
	}

	public List<OrderPartProductItem> getOrderPartProductItems() {
		return orderPartProductItems;
	}

	public void setOrderPartProductItems(
			List<OrderPartProductItem> orderPartProductItems_) {
		orderPartProductItems = orderPartProductItems_;
	}

	public void addOrderPartProductItem(OrderPartProductItem orderItem1_) {
		getOrderPartProductItems().add(orderItem1_);
	}

	public OrderPartProductItem findOrderPartOnSaleProductItemFor(
			OnSaleProduct p_) {
		return findOrderPartProductItemFor(p_.getProduct().getName());
	}

	public OrderPartProductItem findOrderPartProductItemFor(String productName_) {
		for (OrderPartProductItem opi : getOrderPartProductItems()) {
			if (opi.getOnSaleProduct().getProduct().getName().equals(
					productName_)) {
				return opi;
			}
		}
		return null;
	}

	public int indexOfItemFor(OnSaleProduct p_) {
		int i = 0;
		for (OrderPartProductItem opi : getOrderPartProductItems()) {
			if (opi.getOnSaleProduct().equals(p_)) {
				return i;
			}
			i++;
		}
		return -1;
	}

	public boolean isEmpty() {
		return orderPartProductItems.isEmpty()
				&& orderPartMenuItem.getMenuCountAsEnum().equals(
						MenuCount.ZERO_MENU);
	}
	
	public String getWithoutAsString(){
		StringBuilder sb = new StringBuilder();
		for (MenuItem mi : this.getOrderPartMenuItem().getRemovedMenuItems()) {
			if (sb.length() > 0) {
				sb.append("<br>");
			}
			sb.append(mi.getArticleName());
		}
		return sb.toString();
	}
	
	public String getWithAsString(){
		StringBuilder sb = new StringBuilder();
		for (OrderPartProductItem oppi : this.getOrderPartProductItems()) {
			if (sb.length() > 0) {
				sb.append("<br>");
			}
			sb.append(oppi.getQuantity().toString()).append(" ").append(
					oppi.getOnSaleProduct().getProduct().getName());
		}
		return sb.toString();
	}
}
